﻿using System;
using System.Windows.Input;

namespace WordGame
{
    /// <summary>
    /// Delegatecommand,VIEW里面直接使用INTERACTION的trigger激发
    /// </summary>
    public class DelegateCommand : ICommand
    {
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Action"></param>
        public DelegateCommand(Action<object> Action)
        {
            ExecuteAction = Action;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Action"></param>
        /// <param name="Func"></param>
        public DelegateCommand(Action<object> Action, Func<object, bool> Func)
        {
            ExecuteAction = Action;
            CanExecuteFunc = Func;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc == null)
                return true;
            else
                return CanExecuteFunc(parameter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(object parameter)
        {
            ExecuteAction?.Invoke(parameter);
        }

        /// <summary>
        /// 
        /// </summary>
        public Action<object> ExecuteAction { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Func<object, bool> CanExecuteFunc { get; set; }

    }
}
